package com.his.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.his.admin.service.RoleInterfaceService;
import com.his.dao.domain.dto.SelectRoleInterfaceDto;
import com.his.dao.domain.dto.SelectRoleMenuDto;
import com.his.dao.domain.entity.RoleMenu;
import com.his.util.annotation.RequiresLogin;
import com.his.util.annotation.RequiresPermissions;
import com.his.util.entity.RoleInterface;
import com.his.util.result.RestResult;
import com.his.util.result.RestResultBuilder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/roleInterface")
@Api(value = "角色接口模块")
public class RoleInterfaceController {
    @Resource
    private RoleInterfaceService roleInterfaceService;

    @RequiresLogin
    @RequiresPermissions
    @RequestMapping("/all")
    @ApiOperation(value = "获取角色接口列表",notes = "获取角色接口列表")
    public RestResult selectRoleMenu(@RequestBody SelectRoleInterfaceDto selectRoleInterfaceDto) {
        return roleInterfaceService.all(selectRoleInterfaceDto);
    }

    @RequiresLogin
    @RequiresPermissions
    @RequestMapping("/add")
    @ApiOperation(value = "增加角色接口",notes = "增加角色接口")
    public RestResult add(@RequestBody RoleInterface roleInterface) {
        if(roleInterfaceService.save(roleInterface)) {
            int id = roleInterfaceService
                    .getOne(new QueryWrapper<RoleInterface>()
                            .orderByDesc("role_interface_id")
                            .last("limit 1"))
                    .getRoleInterfaceId();
            return new RestResultBuilder().success(id);
        }
        return new RestResultBuilder().fail();
    }

    @RequiresLogin
    @RequiresPermissions
    @RequestMapping("/delete")
    @ApiOperation(value = "删除角色接口",notes = "删除角色接口")
    public RestResult del(@RequestBody RoleInterface roleInterface) {
        if(roleInterfaceService.removeById(roleInterface)) {
            return new RestResultBuilder().success();
        }
        return new RestResultBuilder().fail();
    }
}